﻿package com.badibadi.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONException;

import android.R.integer;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.renderscript.Int2;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.badibadi.activity.BaseActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.infos.AddingRecordNewInfos;
import com.badibadi.infos.Results;
import com.badibadi.infos.UserInfoModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.InterestModel;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

public class AddingRecordNewAdapter extends BaseAdapter {

	/**
	 * 文本大小
	 */
	final int TEXTSIZE = 8;
	private Context context;
	/**
	 * 存放选中的标签的position
	 */
	private Map<Integer, String> TitleMap;

	/**
	 * 记录子类打勾情况
	 */
	private boolean[] isItemClick;
	/**
	 * 记录选中的个数
	 */
	private int selectNumber;
	/**
	 * 选择的上限
	 */
	private int UpperLimit;

	/**
	 * 存储随机颜色
	 */
	private Drawable[] Save_btn_color;
	/**
	 * 存储字体颜色
	 */
	private int[] color_id;

	/**
	 * 标签数量
	 */
	private int label_number;

	private AddingRecordNewInfos arnInfos;

	private List<InterestModel> Interestlist = new ArrayList<InterestModel>();

	public AddingRecordNewAdapter(Context ct, List<InterestModel> Interestlist) {
		context = ct;
		this.Interestlist = Interestlist;
		TitleMap = new Hashtable<Integer, String>();
		arnInfos = new AddingRecordNewInfos(context);
		selectNumber = arnInfos.getSelectNumber();
		UpperLimit = arnInfos.getUpperLimit();
		// item = ct.getResources().getStringArray(R.array.label_type);
		label_number = Interestlist.size();
		Save_btn_color = arnInfos.getSave_btn_color();
		isItemClick = new boolean[Interestlist.size()];
		// isItemClick = new boolean[item.length];
		// for (int i = 0; i < item.length; i++) {
		// isItemClick[i] = false;

		for (int i = 0; i < Interestlist.size(); i++) {
			isItemClick[i] = false;
		}
		color_id = new int[] {
				context.getResources().getColor(R.color.orange_fa7822),
				context.getResources().getColor(R.color.blue),
				context.getResources().getColor(R.color.black_50000000),
				context.getResources().getColor(R.color.black_0) };

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Interestlist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return Interestlist.get(position).getId();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub

		LayoutInflater inflater=LayoutInflater.from(context);
		convertView=inflater.inflate(R.layout.btn_title, null);
		FrameLayout rl = new FrameLayout(context);
        
		Button btn = (Button)convertView.findViewById(R.id.btn_title);
		btn.setText(Interestlist.get(position).getName());
		btn.setTextColor(color_id[position % 3]);
//		btn.setTextSize(DisplayUtil.dip2px(context, TEXTSIZE));
		// btn.setBackgroundColor(Save_btn_color[position%Save_btn_color.length]);
		btn.setBackgroundResource(R.drawable.shape_graye5e5e5_circular_bead);
		btn.setClickable(false);
		btn.setDuplicateParentStateEnabled(true);
		btn.setFocusable(false);
//		rl.addView(btn);
		// 如果此处已经有标记了，那么就添加view
		ImageView iv = (ImageView)convertView.findViewById(R.id.check);
		if (isItemClick[position] == true) {
//			ViewGroup.LayoutParams vl = new ViewGroup.LayoutParams(
//					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//			iv.setBackgroundResource(R.drawable.check_true);
			iv.setVisibility(View.VISIBLE);
//			rl.addView(iv, vl);
		}else
		{
			iv.setVisibility(View.INVISIBLE);
		}

		return convertView;
	}

	public boolean[] getIsItemClick() {
		return isItemClick;
	}

	public void setIsItemClick(boolean[] isItemClick) {
		this.isItemClick = isItemClick;
	}

	/**
	 * 设置改变点击处的位置为（true或者false）(如果当前为true则变成false) 用于判定指定位置是否已经选中(自动添加至map中)
	 * 
	 * @param position
	 */
	public void setIsItemClick(int position) {
		if (this.isItemClick[position] == true) {
			// containsKey(position);检查是否含有该key，有的话就移除
			if (TitleMap.containsKey(position) == true)
				TitleMap.remove(position);
			this.isItemClick[position] = false;
			selectNumber--;
		} else if (this.isItemClick[position] == false) {
			if (TitleMap.containsKey(position) == false)
				TitleMap.put(position, Interestlist.get(position).getId());
			this.isItemClick[position] = true;
			selectNumber++;
		}
	}
    /**
     * 根据id来选中
     * @param id
     */
	public void setIsItemClick_id(String id) {
		for (int i = 0; i < Interestlist.size(); i++) {
			if (id.equals(Interestlist.get(i).getId())) {
				TitleMap.put(i, Interestlist.get(i).getId());
				this.isItemClick[i] = true;
				selectNumber++;
				break;
			}
		}
	}

	/**
	 * 传入key（或position）值获取名字
	 * 
	 * @param position
	 * @return
	 */
	public String getItemName(int position) {
		return Interestlist.get(position).getName();
	}

	public boolean getIsItemClick(int position) {
		return isItemClick[position];
	}

	public int getUpperLimit() {
		return UpperLimit;
	}

	public void setUpperLimit(int upperLimit) {
		UpperLimit = upperLimit;
	}

	public int getSelectNumber() {
		return selectNumber;
	}

	public void setSelectNumber(int selectNumber) {
		this.selectNumber = selectNumber;
	}

	public Map<Integer, String> getTitleMap() {
		return TitleMap;
	}

	public void setTitleMap(Map<Integer, String> titleMap) {
		TitleMap = titleMap;
	}

	public List<InterestModel> getInterestlist() {
		return Interestlist;
	}

	public void setInterestlist(List<InterestModel> interestlist) {
		Interestlist = interestlist;
	}

	public int getMapSize() {
		return Interestlist.size();
	}

}
